Ext.define('Atman.controller.Main', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            main: "#main",
            nav: '#navigation',
            navButton: '#navigationButton',
            chartView: '#chartView',
            viewport: '#viewport',
			chartStore: '#chartstore',
			navigationStore: '#navigationstore',
			gridView: 'gridview'
        },
        control: {
            nav: {
                leafitemtap: 'onNavTap'
            },
            navButton: {
                tap: 'showNav'
            },
            viewport: {
                orientationchange: 'onOrientationChange'
            },
            "#prevButton": {
                tap: "prev"
            },
            "#nextButton": {
                tap: "next"
            },
			chartView:{
				activeitemchange: 'onActiveItemChange'
			}
        }
    },

    prev: function () {
        //this.getChartView().previous();
        Atman.app.GV_count--;
        this.getChartView().setActiveItem(Atman.app.GV_count);
    },

    next: function () {
        //this.getChartView().next();
        Atman.app.GV_count++;
        this.getChartView().setActiveItem(Atman.app.GV_count);
    },
    
    // TODO: remove this after control for 'viewport' is enabled.
    launch: function () {
        Ext.Viewport.onBefore('orientationchange', this.onOrientationChange, this);
        this.onOrientationChange(Ext.Viewport, Ext.Viewport.getOrientation());
    },

    showNav: function () {
        this.getMain().getSheet().show();
    },

    onNavTap: function (buttonx, list, index, item) {
        var me = this,
            record = list.getStore().getAt(index),
            mainView = me.getMain(),
            mainRegion = mainView.getMainRegion(),
            chartView = me.getChartView(),
			gridView = me.getGridView(),
            type = record.parentNode.data.key,
            state = record.data.key,
				myChartStore = Ext.getStore('ChartStore'),
				myUrl = record.data.url, //single
				myQuery = record.data.value.split(','),
				myQName = record.data.qname.split(','),
				myCType = record.data.ctype.split(','),
				mySeries = record.data.series.split(','),
				myCTitle = record.data.ctitle.split(','),
				myNTitle = record.data.ntitle.split(',');

chartView.removeAll(true,false);
/*
console.log('myUrl'+myUrl); //http://atmanr3.dyndns.biz:8001/sap/bc/bsp/sap/zmobile/zbw_mobile_data.json
console.log('myQuery'+myQuery); //ZSD_MOBILE_Q002,ZSD_MOBILE_Q001
console.log('myQName'+myQName); //Query1,Query2
console.log('myCType'+myCType); //area,column
console.log('mySeries'+mySeries); //7,2
console.log('myCTitle'+myCTitle); //Month, Year
console.log('myNTitle'+myNTitle); //Sales, Number
*/

		Atman.app.GV_url = myUrl;
		Atman.app.GV_query = myQuery;
		Atman.app.GV_queryName = myQName;
		Atman.app.GV_chartType = myCType;
		Atman.app.GV_series = mySeries;
		Atman.app.GV_categoryTitle = myCTitle;
		Atman.app.GV_numericTitle = myNTitle;

/*		var getChartPanels = Atman.app.generatePanels(myCType,myQName);
		for(var i=0; i<getChartPanels.length; i++){
			chartView.add(getChartPanels[i]);
		}
*/

        for(var i=0;i<6;i++){
            chartView.add({xtype:'dashboardview'+(i+1)});
        }
		chartView.setActiveItem(0);

        //Ext.getCmp('prevButton').setDisabled(chartView.getActiveIndex() === 0);
        //Ext.getCmp('nextButton').setDisabled(chartView.getActiveIndex() === chartView.getMaxItemIndex());
        
		mainRegion.setActiveItem(chartView, 'slide');
		mainView.setTitle(record.label);
    },

    onOrientationChange: function (viewport, orientation) {
        this.getMain().orientate(orientation);
    },
	onActiveItemChange: function(){
		var me = this,
			mainView = me.getMain(),
            chartView = me.getChartView(),
			myChartStore = Ext.getStore('ChartStore'),
			idx = 0;//chartView.getActiveIndex();
			
		mainView.setMasked({
				xtype: 'loadmask',
				message: 'Loading...'
		});

		myChartStore._proxy._url = Atman.app.GV_url;
		myChartStore._proxy._extraParams.query = Atman.app.GV_query[idx];
		myChartStore.load(function(records, operation, success) {
			if(success){
				var LV_strHeaderField = '',
					LV_arrFieldName = [],
					LV_arrFieldValue = [],
					count = 1;

				for ( var p in records[0].data ){
					if(count > Atman.app.GV_series[idx]){break;}
					if( p == 'category') {
						LV_strHeaderField = records[0].data[p];
					}
					
					LV_arrFieldValue.push(records[0].data['series'+(count)]);
					LV_arrFieldName.push('series'+(count));
					
					count++;
				}

				Atman.app.GV_records = records;
				Atman.app.GV_strHeaderField = LV_strHeaderField;
				Atman.app.GV_arrFieldName = LV_arrFieldName;
				Atman.app.GV_arrFieldValue = LV_arrFieldValue;

				//records.splice(0,1);
				var lvRecords = [];
				for(var i=0; i<records.length; i++){
					if(i != 0){lvRecords.push(records[i]);}
				}

				/* 차트 중복생성 방지 */
/*				if(chartView.getActiveItem().items.length > 1){}//0:toolbar, 1:chart, 2:legend
				else{
					console.log('생성!');
					var lvArr = [Atman.app.GV_numericTitle[idx],Atman.app.GV_categoryTitle[idx],Atman.app.GV_chartType[idx],Atman.app.GV_series[idx]];
					var getChart = Atman.app.generateChart(LV_arrFieldName,LV_arrFieldValue,lvArr);
					chartView.getActiveItem().add(getChart);
				}
*/
				Ext.getStore('ChartStore').setData(lvRecords);
				setTimeout(function() { mainView.unmask();}, 500);
			}
			else{
				mainView.setMasked({
					msg: 'Failed loading!'
				});
			}
		});
	}
});